import collections
import copy
class Solution:
    def findShortestCycle(self, n: int, edges) -> int:
        d = collections.defaultdict(set)
        du = collections.defaultdict(int)
        for u, v in edges:
            du[u] += 1
            du[v] += 1
            d[u].add(v)
            d[v].add(u)
        now_st = set()
        for node in du:
            if du[node] == 1:
                now_st.add(node)
        while now_st:
            next_st = set()
            for node in now_st:
                for v in d[node]:
                    du[v] -= 1
                    if du[v] == 1:
                        next_st.add(v)
            now_st = next_st
        new_du = set()
        for key in du:
            if du[key] >= 2:
                new_du.add(key)
        if not new_du:
            return -1
        m = len(new_du)
        ans = float('inf')
        for start in new_du:
            dist_list = {item: -1 for item in new_du}
            new_d = copy.deepcopy(d)
            dist = 0
            now = {start}
            while now:
                dist += 1
                next_st = set()
                temp_ans = 0
                for node in now:
                    for v in new_d[node]:
                        if v in dist_list:
                            if dist_list[v] == -1:
                                dist_list[v] = dist
                                next_st.add(v)
                                new_d[v].remove(node)
                            else:
                                temp_ans = 1 + dist_list[node] + dist_list[v]
                                break
                    if temp_ans:
                        break
                if temp_ans:
                    ans = min(ans, temp_ans)
                    break
                now = next_st
        return ans

data = Solution()
n = 4
edges = [[0,1],[0,2],[1,2],[2,3],[1,3]]
print(data.findShortestCycle(n, edges))